package com.example.woke;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.adapter.Mybankbadter;
import com.woke.data.Data_bindcarlist;
import com.woke.method.MyApp;
import com.wokeMy.view.OpenBusinessActivity;
import com.wokeMy.view.model.Constant;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MybankblindActivity extends Activity implements OnItemClickListener {

	private ListView mListView;
	private LayoutInflater inflater;
	//	private TextView mTadd;
	private MyApp application;
	private ArrayList<String> cardlist = new ArrayList<String> ();
	private ArrayList<Data_bindcarlist> datalist = new ArrayList<Data_bindcarlist> ();
	private RelativeLayout mBar;
	private ArrayList<String> list = new ArrayList<String> ();
	private String czhiortxian;
	private Intent myintent;
	private Mybankbadter adapter;
	private boolean addbank;//�����ȥ�󿨳ɹ����ж��Ƿ���Ҫ����ˢ��

	private float money;
	String	passway,idCard;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView (R.layout.activity_mybankblind);

		inflater = getLayoutInflater ();
		application = (MyApp) getApplication ();
		myintent = getIntent ();
		//chongzhiΪ�а���ת
		//tixianΪ�а���ת
		czhiortxian = myintent.getStringExtra ("czhiortxian");//noʱΪ����벻�漰���ֺͳ�ֵ
		intview ();

	}

	private void intview() {

		mBar = (RelativeLayout) findViewById (R.id.avmybankb_progres_bar);
		mListView = (ListView) findViewById (R.id.avmybankb_list_view);
		mListView.setOnItemClickListener (this);

		findViewById (R.id.avmybankb_text_add).setOnClickListener (new OnClickListener () {

			@Override
			public void onClick(View v) {
				if (datalist.size()==0){
					idCard="";
				}else {
					idCard=datalist.get(0).idcard;
				}

				if (czhiortxian.equals ("chongzhi")) {
					Intent intent = new Intent (MybankblindActivity.this, MywalletcardActivity.class);
					intent.putExtra ("czhiortxian", "chongzhi1");
					intent.putExtra ("idcard", idCard);
					startActivity (intent);
					finish ();
				} else if (czhiortxian.equals ("tixian")) {
					Intent intent = new Intent (MybankblindActivity.this, MywalletcardActivity.class);
					intent.putExtra ("czhiortxian", "tixian1");
					intent.putExtra ("idcard", idCard);
					startActivity (intent);
					finish ();
				} else if (czhiortxian.equals ("nocarpay")) {
					Intent intent = new Intent (MybankblindActivity.this, MywalletcardActivity.class);
					intent.putExtra ("czhiortxian", "nocarpay");

					startActivity (intent);
					finish ();
				} else {
					Intent intent = new Intent (MybankblindActivity.this, MywalletcardActivity.class);
					intent.putExtra ("czhiortxian", "no");
					startActivityForResult (intent, 13);
				}
			}
		});

		findViewById (R.id.avmybankbilid_image_back).setOnClickListener (new OnClickListener () {

			@Override
			public void onClick(View v) {
				finish ();
			}
		});
		adapter = new Mybankbadter (inflater,datalist);
		mListView.setAdapter (adapter);
		getbanklist ();

	}

	private void getbanklist() {
		if (application.getDatas_load () == null) return;
		AsyncHttpClient ahc = new AsyncHttpClient ();
		PersistentCookieStore myCookieStore = new PersistentCookieStore (
				MybankblindActivity.this);
		ahc.setCookieStore (myCookieStore);
		RequestParams params = new RequestParams ();
		params.put ("g", "api");
		params.put ("m", "BankList");
		params.put ("a", "getBankList");
		params.put ("userid", application.getDatas_load ().getId ());
//		params.put("userid", "8");
//		Log.e(application.getuserid(), application.getuserid());
		ahc.get (HttpMethods.BASEURL, params, new JsonHttpResponseHandler () {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess (statusCode, headers, response);
				Log.e ("MybankblindActivity", "response" + response);
				try {
					String flag = response.getString ("flag");
					String info = response.getString ("info");
					if (flag.equals ("success")) {
						JSONObject jsonObject = response.getJSONObject ("data");
						String string = jsonObject.getString ("data");
//						ArrayList<Data_bindcarlist> parseArray = (ArrayList<Data_bindcarlist>) com.alibaba.fastjson.JSONArray.parseArray(string, Data_bindcarlist.class);
//						datalist.addAll(parseArray);
						JSONArray cardlist = jsonObject.getJSONArray ("data");
						for (int i = 0; i < cardlist.length (); i++) {
							JSONObject carinfo = cardlist.getJSONObject (i);
							String id = carinfo.getString ("id");
							String phone = carinfo.getString ("phone");
							String brname = carinfo.getString ("brname");
							String name = carinfo.getString ("name");
							String validthru = carinfo.getString ("validthru");
							String idcard = carinfo.getString ("idcard");
							String cardno = carinfo.getString ("cardno");
							String user_id = carinfo.getString ("user_id");
							String code = carinfo.getString ("code");
							String type = carinfo.getString ("type");
							String identityid = carinfo.getString ("identityid");
							String bindid = carinfo.getString ("bindid");

							Data_bindcarlist list = new Data_bindcarlist (id, phone, brname,
									name, validthru, idcard, cardno, user_id, code, type, identityid, bindid);

							datalist.add (list);
						}
					}
				} catch (JSONException e) {
					e.printStackTrace ();
				}
				if (datalist.size () > 0) {

					adapter.notifyDataSetChanged ();
				} else if (datalist.size () > 0 && addbank == true) {
				}
//				else{
//					mTok.setText("�����п�");
//					cardbank = false;
//				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure (statusCode, headers, responseString, throwable);
				Toast.makeText (MybankblindActivity.this, "数据失败！",
						Toast.LENGTH_SHORT).show ();
//				Log.e("��", "ʧ��" + responseString);
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart ();
				mBar.setVisibility (View.VISIBLE);
//				mTok.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_lightgray));
//				mTok.setClickable(false);
			}

			@Override
			public void onFinish() {
				super.onFinish ();
				mBar.setVisibility (View.GONE);
//				mTok.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_lightred));
//				mTok.setClickable(true);
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent ();
		//�������֤�����š��ֻ�����
		list.clear ();
		Data_bindcarlist listdata = datalist.get (arg2);
		list.add (listdata.name);// 名字����
		list.add (listdata.idcard);//�身份证֤
		list.add (listdata.phone);//�电话
		list.add (listdata.cardno);//��卡号��
		list.add (listdata.brname);//��银行名字�п��������
		list.add (listdata.type);// 类型 信用卡 储蓄卡
		list.add (listdata.code);// cvv
		list.add (listdata.validthru);// 有效期
		Log.e("onItemClick",czhiortxian);
		if (czhiortxian.equals ("chongzhi")) {
			intent.putExtra ("chongzhi1", list);
			setResult (93, intent);
			finish ();
		} else if (czhiortxian.equals ("tixian")) {
			if(listdata.type.equals("1")){  // 0 是信用卡 1是储蓄卡
				intent.putExtra ("tixian1", list);
				setResult (96, intent);
				finish ();
			}else {
				Toast.makeText(MybankblindActivity.this,"提现要为储蓄卡",Toast.LENGTH_SHORT).show();
			}
		} else if (czhiortxian.equals ("nocarpay")) {
//			intent.putExtra ("nocarpay", list);
//			setResult (97, intent);
//			finish ();
			passway=getIntent().getStringExtra("passway");
			money=getIntent().getFloatExtra("money",0.0f);
			Intent intent1=new Intent(this, OpenBusinessActivity.class);
			intent1.putExtra ("nocarpay", list);
			intent1.putExtra ("money", money);
			intent1.putExtra ("passway", passway);
			startActivity(intent1);
		} else if(czhiortxian.equals ("zhifucard")){
			if(listdata.type.equals("0")){
//				intent.putExtra ("zhifucard", list);
				intent.putExtra("listdata",listdata);
				setResult (Constant.RESPONSE_ZHIFU, intent);
				finish ();
			}else {
				Toast.makeText(MybankblindActivity.this,"付款卡要为信用卡",Toast.LENGTH_SHORT).show();
			}


		} else if(czhiortxian.equals ("zscard")){
			if(listdata.type.equals("1")){
				//				intent.putExtra ("zhifucard", list);
				intent.putExtra("listdata",listdata);
				setResult(Constant.RESPONSE_JIESHUAN, intent);
				finish ();
			}else {
				Toast.makeText(MybankblindActivity.this,"收款卡要为储蓄卡",Toast.LENGTH_SHORT).show();
			}


		} else if(czhiortxian.equals ("wu")){
				//				intent.putExtra ("zhifucard", list);
				intent.putExtra("listdata",listdata);
				setResult(Constant.RESPONSE_JIESHUAN, intent);
				finish ();

		} else if (czhiortxian.equals ("rhlz")){
			// 融汇入驻
			if(listdata.type.equals("1")){
				//				intent.putExtra ("zhifucard", list);
				intent.putExtra("listdata",listdata);
				setResult(Constant.RESPONSE_RHLZ, intent);
				finish ();
			}else {
				Toast.makeText(MybankblindActivity.this,"结算卡要为储蓄卡",Toast.LENGTH_SHORT).show();
			}
		} else if (czhiortxian.equals ("rhkjzf")){
			// 融汇快捷支付
			if(listdata.type.equals("0")){
				//				intent.putExtra ("zhifucard", list);
				intent.putExtra("listdata",listdata);
				setResult(Constant.RESPONSE_RHKJZF, intent);
				finish ();
			}else {
				Toast.makeText(MybankblindActivity.this,"支付卡要为信用卡",Toast.LENGTH_SHORT).show();
			}


		}
		else if (czhiortxian.equals ("huixiangrz")){
			//汇享入驻
			if(listdata.type.equals("1")){
				//				intent.putExtra ("zhifucard", list);
				intent.putExtra("listdata",listdata);
				setResult(Constant.RESPONSE_HXRZ, intent);
				finish ();
			}else {
				Toast.makeText(MybankblindActivity.this,"结算卡要为储蓄卡",Toast.LENGTH_SHORT).show();
			}

		} else if (czhiortxian.equals ("huixzf")){
			// 汇享快捷支付
			if(listdata.type.equals("0")){
				//				intent.putExtra ("zhifucard", list);
				intent.putExtra("listdata",listdata);
				setResult(Constant.RESPONSE_HXKJZF, intent);
				finish ();
			}else {
				Toast.makeText(MybankblindActivity.this,"支付卡要为信用卡",Toast.LENGTH_SHORT).show();
			}


		} else if (czhiortxian.equals ("creditcard")){
//			// 掌财通 信用卡代还
//			if(listdata.type.equals("0")){
//				//				intent.putExtra ("zhifucard", list);
//				intent.putExtra("listdata",listdata);
//				intent.putExtra("card",myintent.getStringExtra("card"));
//				setResult(Constant.RESPONSE_ZCTCREPLAY, intent);
//				finish ();
//			}else {
//				Toast.makeText(MybankblindActivity.this,"请选信用卡",Toast.LENGTH_SHORT).show();
//			}
			// 高汇通 信用卡代还
				intent.putExtra("listdata",listdata);
				intent.putExtra("card",myintent.getStringExtra("card"));
				setResult(Constant.RESPONSE_ZCTCREPLAY, intent);
				finish ();

		}else if (czhiortxian.equals ("mobaoAll")){
			// 魔宝
				intent.putExtra("listdata",listdata);
				intent.putExtra("card",myintent.getStringExtra("card"));
				setResult(Constant.RESPONSE_ZHIFU, intent);
				finish ();
			//采用  zhifucard 请求吗 结果吗
		} else {
			intent.setClass (MybankblindActivity.this, JiebangActivity.class);
			intent.putExtra ("no", list);
			Log.e("list", ""+list);
			intent.putExtra ("currentno", arg2);
			startActivityForResult (intent, 12);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult (requestCode, resultCode, data);

		if (requestCode == 12 && resultCode == 11) {
			int intExtra = data.getIntExtra ("currentno", - 1);
			if (intExtra > - 1) {
				datalist.remove (intExtra);
				list.clear ();
				adapter.notifyDataSetChanged ();
			}
//			finish();
		} else if (requestCode == 13 && resultCode == 14) {//������󿨳ɹ�����
			addbank = true;
			datalist.clear ();
			getbanklist ();
		}

	}

}
